package com.ls.sort_algorithm;

import java.util.Date;

/**
 * 假设第一个是最小的
 * 依次寻找下一个与最小比较
 * 记录最小的索引
 * 交换(剩余待判断长度-1)
 */
public class SelectSort {
    public static void main(String[] args) {
        SelectSort selectSort = new SelectSort();
//        int[] arr = {3, 9, -1, -2, 10};

        int[] arr = new int[800000];
        for (int i = 0; i < 800000; i++) {
            arr[i] = (int) (Math.random() * 8000000);
        }

        System.out.println(new Date());
        selectSort.selectSort(arr);
        System.out.println(new Date());

    }

    public void selectSort(int[] arr) {


        for (int i = 0; i < arr.length - 1; i++) {
            int mixIndex = i;
            int min = arr[i];
            for (int j = i + 1; j < arr.length; j++) {
                if (min > arr[j]){
                    min = arr[j];
                    mixIndex = j;
                }
            }

            if (mixIndex != i){
                arr[mixIndex] = arr[i];
                arr[i] = min;
            }
//            System.out.println(Arrays.toString(arr));
        }


    }
}
